package se.webbzon.boltzmann.editor.factory.propertyframe;

import java.awt.GridLayout;
import java.util.Collection;
import java.util.SortedSet;

import javax.swing.BorderFactory;
import javax.swing.BoxLayout;
import javax.swing.JColorChooser;
import javax.swing.JComboBox;
import javax.swing.JLabel;
import javax.swing.JPanel;

import se.webbzon.boltzmann.editor.component.portal.PortalPanel;
import se.webbzon.boltzmann.editor.component.texturelist.TextureSetEntry;
import se.webbzon.boltzmann.editor.component.texturelist.TextureSetList;
import se.webbzon.boltzmann.editor.mvc.EditorModel;
import se.webbzon.boltzmann.editor.propertyframe.WorldObjectPropertyFrame;
import se.webbzon.boltzmann.editor.propertyframe.WorldStaticObjectPropertyFrame;
import se.webbzon.boltzmann.game.object.portal.DoorEnum;
import se.webbzon.boltzmann.game.object.portal.DoorPortal;
import se.webbzon.oschi01.worldobject.WorldObject;

public class DoorPortalPropertyFrameFactory implements PropertyFrameFactory {

	private final SortedSet<TextureSetEntry> textures;
	
	/*============================================================
	Constructors
	============================================================*/
	
	public DoorPortalPropertyFrameFactory(SortedSet<TextureSetEntry> textures) {
		this.textures = textures;
	}
	
	/*============================================================
	Public Methods
	============================================================*/
	
	@Override public String getWorldObjectName() {
		return DoorPortal.worldObjectName;
	}

	@Override public WorldObjectPropertyFrame makePropertyFrame(EditorModel model,
			WorldObject instance) {
		return new PropertyFrameImpl(model,textures, (DoorPortal) instance);
	}
	
	/*============================================================
	Private Classes
	============================================================*/
	
	private static final class PropertyFrameImpl extends WorldStaticObjectPropertyFrame {

		// The portal panel of this property frame
		private final PortalPanel portalInput;
		
		// The texture set list of this property frame
		private final TextureSetList textureInput;
		
		// The color choosers of this property frame
		private final JColorChooser blendingInput;
		
		// The combo boxers of this property frame
		private final JComboBox<DoorEnum> doorInput;
		
		/*============================================================
		Constructors
		============================================================*/
		
		public PropertyFrameImpl(	EditorModel model, 
									SortedSet<TextureSetEntry> textures, 
									DoorPortal instance) {
			super(model, instance);
			
			// The door portal panel
			JPanel settings = new JPanel();
			settings.setLayout(new BoxLayout(settings,BoxLayout.Y_AXIS));
			
			JPanel doorTypePanel = new JPanel();
			doorTypePanel.setLayout(new GridLayout(1,2));
			doorTypePanel.setBorder(BorderFactory.createTitledBorder("Door Type:"));
			doorInput = new JComboBox<DoorEnum>(DoorEnum.values());
			doorInput.setSelectedItem(instance.getDoorType());
			doorTypePanel.add(new JLabel("Door Type:"));
			doorTypePanel.add(doorInput);
			settings.add(doorTypePanel);
			
			JPanel doorBlendingPanel = new JPanel();
			doorBlendingPanel.setBorder(BorderFactory.createTitledBorder("Texture Blending:"));
			blendingInput = new JColorChooser(instance.getTextureBlending());
			doorBlendingPanel.add(blendingInput);
			settings.add(doorBlendingPanel);
			
			addPanel("Door Settings",settings);
			
			// The portal panel
			portalInput = new PortalPanel(instance);
			addPanel(portalInput.getName(),portalInput);
			
			// The texture panel
			textureInput = new TextureSetList(textures,instance.getTextureSetName(),0,0);
			addPanel(textureInput.getName(),textureInput);
			
			initialize();
		}
		
		/*============================================================
		Public Methods
		============================================================*/
		
		@Override public boolean apply(Collection<String> errorMessages) {
			if (super.apply(errorMessages)) try {
				DoorPortal portal = (DoorPortal) getInstance();
				portal.setPortalId(portalInput.getPortalId());
				portal.setWorldAreaName(portalInput.getWorldAreaName());
				portal.setTextureSetName(textureInput.getTextureSetName());
				portal.setTextureBlending(blendingInput.getColor());
				portal.setDoorType((DoorEnum) doorInput.getSelectedItem());
				return true;
			} catch (NumberFormatException e) {
				errorMessages.add(e.getMessage());
				return false;
			} else
				return false;
		}
		
	}

}
